package com.ziyia.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.renderscript.ScriptIntrinsicBlend;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.Console;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

public class MainActivity extends AppCompatActivity {

    // 负责更新进度的 Handler
    private MyHandler myHandler = new MyHandler(new WeakReference<>(this));
    // 进度条
    private ProgressBar bar;
    // 当前进度
    private int status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bar = findViewById(R.id.press);

        new Thread(() -> {
            while (status < 100) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                status++;
                myHandler.sendEmptyMessage(0x111);
            }
        }).start();




    }

    public void down(View view) {

    }




    class MyHandler extends Handler {
        private WeakReference<MainActivity> activity;


        public MyHandler(WeakReference<MainActivity> activity) {
            this.activity = activity;
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 0x111) {
                activity.get().bar.setProgress(activity.get().status);
//                activity.get().bar.incrementProgressBy(status);
            }
        }
    }
}